% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

%% @author CA Meijer
%% @copyright 2012 CA Meijer
%% @doc The Nimbula application.
%%      
-module(nimbula_app).

-behaviour(application).

%% External exports
-export([start/0]).

%% Behavioural exports
-export([start/2,
		 stop/1]).

%% API
start() ->
	start(nimbula).

%% Behavioural functions
%% @doc Starts the application. This function creates an ETS table that is 
%%      passed to the nimbula supervisor. The table is used to store identfiers
%%      for HTTP sessions that are established.
-spec(start(any(), any()) -> {ok, pid()} | {error, Reason::any()}).
start(_Type, _StartArgs) ->
	EtsId = ets:new(nimbula_table, [public]),
	ets:insert(EtsId, {number_of_ids, 0}),
	ets:insert(EtsId, {available_ids, []}),
	nimbula_sup:start_link(EtsId).

%% @doc Stops the application.
-spec(stop(any()) -> ok).
stop(_State) ->
    ok.


%% Internal functions
start(Application) ->
	case application:start(Application) of
		{error, {not_started, Dependency}} ->
			start(Dependency),
			start(Application);
		Result ->
			Result
	end.
